package com.runa.rccprotocol.dto.runa;

import com.runa.protocol.dto.DtuHDRequestData;
import com.runa.protocol.dto.DtuReportAck;
import com.runa.protocol.dto.IResponseData;
import com.runa.protocol.enums.DtuModel;
import com.runa.protocol.util.ByteUtil;
import com.runa.rccprotocol.dto.ConcentratorRequestData;
import com.runa.rccprotocol.dto.equdata.EquData;
import org.springframework.util.StringUtils;

import java.util.List;

public class RunaLoraRequestData extends ConcentratorRequestData {

    private byte[] requestBytes;

    private String serial;

    public RunaLoraRequestData(String address) {
        super(address);
    }

    public void readHeatMeter(String meter, String path) {
        RunaDeviceRequestData requestData = new RunaDeviceRequestData();
        requestData.readHeatMeter(meter);
        loraBytes(meter, path, requestData.getData());
        this.serial = meter;
        setHaveResult(true);
    }

    public void readBuildValve(String valve, String path) {
        RunaDeviceRequestData requestData = new RunaDeviceRequestData();
        requestData.readBuildValve(valve);
        loraBytes(valve, path, requestData.getData());
        this.serial = valve;
        setHaveResult(true);
    }

    public void setBuildValveOpen(String valve, int open, String path) {
        RunaDeviceRequestData requestData = new RunaDeviceRequestData();
        requestData.setBuildValveOpen(valve, open);
        loraBytes(valve, path, requestData.getData());
        this.serial = valve;
        setHaveResult(true);
    }

    public void setBuildValveModel(String valve, boolean model, String path) {
        RunaDeviceRequestData requestData = new RunaDeviceRequestData();
        requestData.setBuildValveModel(valve, model);
        loraBytes(valve, path, requestData.getData());
        this.serial = valve;
        setHaveResult(true);
    }

    private byte[] comBytes() {
        return new byte[]{0x3d, 0x3d, 0x3d, 6, 0, 0, 0, 0};
    }

    private void loraBytes(String target, String path, byte[] msgData) {
        String[] paths = new String[0];
        if (StringUtils.isEmpty(path)) {
            requestBytes = new byte[19 + msgData.length];
        } else {
            paths = path.split("/");
            requestBytes = new byte[19 + msgData.length + (4 * paths.length)];
        }
        requestBytes[0] = 0x2a;
        requestBytes[1] = 0x2a;
        requestBytes[2] = (byte) requestBytes.length;
        requestBytes[3] = 0;
        requestBytes[4] = 0;
        requestBytes[5] = (byte) paths.length;
        ByteUtil.tenstringToBcd(requestBytes, address, 6, false);
        int index = 10;
        for (String string : paths) {
            ByteUtil.tenstringToBcd(requestBytes, string, index, false);
            index += 4;
        }
        ByteUtil.tenstringToBcd(requestBytes, target, index, false);
        index += 8;
        System.arraycopy(msgData, 0, requestBytes, index, msgData.length);
        index += msgData.length;
        requestBytes[index] = ByteUtil.sum(requestBytes, 0, requestBytes.length-1);
        requestBytes = ByteUtil.merge(comBytes(), requestBytes);
    }

    @Override
    public boolean check(IResponseData data) {
//        if (data instanceof RunaConcenResponseData) {
//            List<EquData> equData = (List<EquData>) data.getData();
//            if (!equData.isEmpty()) {
//                return serial.equals(equData.get(0).getSerial());
//            }
//            return false;
//        }
        return true;
    }

    @Override
    public byte[] getData() {
        return requestBytes;
    }

    public static void main(String[] args) {
        RunaLoraRequestData requestData = new RunaLoraRequestData("77000001");
        requestData.readBuildValve("80000118", "");

        System.out.println(ByteUtil.byteToHexString(requestData.getData()));
        DtuHDRequestData hdRequestData = new DtuHDRequestData("77000001", requestData);
        System.out.println(ByteUtil.byteToHexString(hdRequestData.getData()));

        requestData.setBuildValveOpen("80000118", 80, "");
        System.out.println(ByteUtil.byteToHexString(hdRequestData.getData()));

        DtuReportAck ack = DtuReportAck.createAck(DtuModel.HongDian7710, "77000001");
        System.out.println(ByteUtil.byteToHexString(ack.getData()));
    }
}
